﻿using markdown_picture_packer.common.constants;
using markdown_picture_packer.common.utils;
using markdown_picture_packer.frontend;
using markdown_picture_packer.frontend.forms;
using markdown_picture_packer.frontend.panels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace markdown_picture_packer
{
    public partial class MainForm : BaseForm
    {
        private FilePathPanel Fpp;
        private MakeNamePanel Mnp;
        private ExportPathPanel Epp;
        private Button Startbtn;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Epp.SelectComboBox(1);
        }
        public override void InitComponent()
        {
            Fpp = new FilePathPanel();
            Mnp = new MakeNamePanel();
            Epp = new ExportPathPanel();
            Startbtn = new Button
            {
                Text = "开始打包",
                Font = new Font("微软雅黑", 20, FontStyle.Bold),
                BackColor = Color.Green,
                ForeColor = Color.White,
            };
        }
        public override void InitSize(int newWdith, int newHeight)
        {
            Size newSize = new Size(newWdith, Constants.MAIN_LINE_HEIGHT);
            Fpp.Size = newSize;
            Mnp.Size = newSize;
            Epp.Size = newSize;
            Startbtn.Size = new Size(newWdith, FrontUtil.GetFillHeight(newHeight, Fpp, Mnp, Epp) - 9);
        }
        public override void InitLocation()
        {
            Fpp.Location = new Point(0, 0);
            FrontUtil.OnBottom(Mnp, Fpp, 3);
            FrontUtil.OnBottom(Epp, Mnp, 3);
            FrontUtil.OnBottom(Startbtn, Epp, 3);
        }

        public override void AddComponent()
        {
            Controls.Add(Fpp);
            Controls.Add(Mnp);
            Controls.Add(Epp);
            Controls.Add(Startbtn);
        }

        public override void InitEvent()
        {
            base.InitEvent();
            Startbtn.Click += (s, e) =>
            {
                if (!TryGetValidPath(Fpp.GetPath, "文件路径不存在!!!", out string filePath)) return;
                if (!TryGetValidPath(Epp.GetPath, "导出位置不存在!!!", out string exportPath)) return;
                List<string> filePaths = new List<string>();
                try
                {
                    if (Fpp.IsChecked()) filePaths.AddRange(Directory.EnumerateFiles(filePath, "*.md", SearchOption.AllDirectories));
                    else filePaths.Add(filePath);
                }
                catch (Exception ex) when (ex is UnauthorizedAccessException || ex is DirectoryNotFoundException || ex is PathTooLongException)
                {
                    MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                ExecuteForm ef = new ExecuteForm(filePaths, exportPath, Mnp.GetSelectText())
                {
                    StartPosition = FormStartPosition.CenterParent,
                    FormBorderStyle = FormBorderStyle.FixedSingle,
                    Icon = Properties.Resources.Logo,
                    MaximizeBox = false,
                    Text = "执行结果与详情",
                    Size = new Size(FrontUtil.GetRealWidth(Width), Height - 10)
                };
                ef.ShowDialog();
            };
        }
        private bool TryGetValidPath(Func<string> pathGetter, string errorMessage, out string path)
        {
            path = pathGetter();
            if (path != null) return true;
            MessageBox.Show(errorMessage, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return false;
        }
    }
}
